<?php

namespace app\admin\controller\goods;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="goods_category")
 */
class Category extends AdminController
{

    use \app\admin\traits\Curd;
    protected $relationSearch = true;
    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\GoodsCategory();
        
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
//            if ($this->user['role'] == 1) {
//                $shopIds = \app\common\model\Shop::where('user_id', $this->user['id'])->column('id');
//                if ($shopIds) {
//                    $where[] = ['shop.id', 'in', $shopIds];
//                }
//            }
//            $count = $this->model
//                ->withJoin('shop','left')
//                ->where($where)
//                ->count();
            $list = $this->model
                ->withJoin('shop','left')
                ->where($where)
//                ->page($page, $limit)
                ->order($this->sort)
                ->select()->toArray();
            $newList = [];
            $repeatString = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            foreach($list as $k=>$l){
                if(!$l['shop']){
                    $l['shop']['name'] = '平台';
                }
                if($l['pid'] == 0){
                    $newList[] = $l;
                    foreach($list as $kk => $vv){
                        if($vv['pid'] != 0 && $vv['pid'] == $l['id']){
                            if(!$vv['shop']){
                                $vv['shop']['name'] = '平台';
                            }
                            $vv['name'] = "{$repeatString}├{$repeatString}" . $vv['name'] ;
                            $newList[] = $vv;
                        }
                    }
                }
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => 0,
                'data'  => $newList,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            //如果添加的是下级 整理父类id
            if(input('id')){
                $post['pid'] = input('id');
            }
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        $shops = [];
        if ($this->user['role'] == 1) {
            $this->assign('is_shop', 1);
            $shops = \app\common\model\Shop::where('user_id', $this->user['id'])->where('status',1)->field('id,name')->select()->toArray();
        }else{
            $this->assign('is_shop', 0);
        }

        $this->assign('shops', $shops);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);

        $shops = [];
        if ($this->user['role'] == 1) {
            $this->assign('is_shop', 1);
            $shops = \app\common\model\Shop::where('user_id', $this->user['id'])->where('status',1)->field('id,name')->select()->toArray();
        }else{
            $this->assign('is_shop', 0);
        }

        $this->assign('shops', $shops);
        return $this->fetch();
    }
}